import util from "../../util";
import parent from '../parent/parent';

export default class mesh extends parent{
    constructor(path) {
        super(mesh, util.api.Mesh);
        this.file = null
        this.fileBase64 = null
    }
    static create(record, path){
        let object=new mesh(path);
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record=this.record){
        // if(!record||!record.type||!record.name||record.type==''||record.name==''){
        //     return false;
        // }
        let newRecord=Object.assign(this.clz.tpl(),record);
        let result = await this.api.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    // 下载
    async downloadFileByName(name=this.record.name){
        let result = await this.api.downloadFileByName(name).then(data=>{
            console.log("file: mesh.js:41  mesh  downloadFileByName", this.record,name, data)
            if(!!data){
                this.file = data
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async downloadFileBase64ByName(name=this.record.name){
        console.log("file: mesh.js:41  mesh  downloadFileByName  this.record:", this.record,name)
        let result = await this.api.downloadFileBase64ByName(name).then(data=>{
            if(!!data){
                this.fileBase64 = data
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async downloadFileById(id=this.record.id){
        let result = await this.api.downloadFileById(id).then(data=>{
            if(!!data){
                this.file = data
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async downloadFileBase64ById(id=this.record.id){
        let result = await this.api.downloadFileBase64ById(id).then(data=>{
            if(!!data){
                this.fileBase64 = data
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    // 删除文件
    async deleteFileById(id=this.record.id){
        let result = await this.api.deleteFileById(id).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async deleteFileByName(name=this.record.name){
        let result = await this.api.deleteFileByName(name).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    // 上传文件
    async uploadFile(file, id){
        let result = await this.api.uploadFile(file, id).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /* 新增模板 */
    static tpl(){
        return {
            id: null,
            type: "",
            name: "",
            version: "1.0",
            published: true,
            comments: "",
        }
    }
}
